package com.hongdee.crm.task.dao.impl;

import com.hongdee.base.basedao.impl.BaseDaoImpl;
import com.hongdee.crm.task.dao.IBcMaintainTypeTaskDao;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 名  称: BcMainTainTypeTaskDaoImpl
 * 类  型: class
 * 描  述: 保养类任务相关
 * 路  径: com.hongdee.crm.task.dao.impl
 * 作  者: wangshuai
 * 日  期:2017/2/28 10:23
 */
@Service
public class BcMainTainTypeTaskDaoImpl extends BaseDaoImpl implements IBcMaintainTypeTaskDao {
    @Override
    public List<Map<String, Object>> findCompanyList() throws Exception {
        List<Map<String,Object>> companyList=this.getJdbcDao().queryForMapListBySql("select company_id from t_bc_maintain_type GROUP BY company_id");
        if(companyList.size()>0){
            return companyList;
        }
        return null;
    }

    /**
     * 索赔类别查询所有设置的公司信息
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String,Object>> findCompanyBySpList() throws Exception {
        List<Map<String,Object>> companyList=this.getJdbcDao().queryForMapListBySql("select company_id from t_crm_wo_sp_type group by company_id");
        if(companyList.size()>0){
            return companyList;
        }
        return null;
    }


    /****
     *
     * 查询关系不对，通过公司和关联类别判断
     *
     * **/
    @Override
    public List<Map<String, Object>> findMaintainTypeByComIdList(String companyId) throws Exception {
        String sqlStr="select maint_p_name, " +
                "    case                    " +
                "    when (maint_type_name <>'' && maint_type_name is not NULL ) then maint_type_name  " +
                "    else maint_project_name   " +
                "    end  maint_project_name" +
                ",maint_p_code,maint_ref,maint_p_code from t_bc_maintain_type where company_id=?";
        List<Map<String,Object>> mainTainTypeList=this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr,companyId);
        if(mainTainTypeList.size()>0){
            return mainTainTypeList;
        }
        return null;
    }

    /**
     * 获得索赔类别
     * @param companyId
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String,Object>> findSpTypeByComIdList(String companyId) throws Exception {
        String sql="select ref_value as maint_project_name,sp_type maint_p_name,ref_type maint_ref,company_id,`status` from t_crm_wo_sp_type where company_id=?";
        List<Map<String,Object>> mainTainTypeList=this.getJdbcDao().queryForMapListBySqlWithParams(sql,companyId);
        if(mainTainTypeList.size()>0){
            return mainTainTypeList;
        }
        return null;
    }

    @Override
    public List<Map<String,Object>> findMaintainTypeByComIdAndTypeList(String companyId,String type) throws Exception {
        String sqlStr="select maint_p_name,maint_project_name,maint_p_code,maint_ref,maint_p_code from t_bc_maintain_type" +
                " where company_id=? and maint_p_name=? and status='1'";
        List<Map<String,Object>> mainTainTypeList=this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr,companyId,type);
        if(mainTainTypeList.size()>0){
            return mainTainTypeList;
        }
        return null;
    }

    @Override
    public List<Map<String,Object>> findSetMaintainType(String companyId,String code) throws Exception {
        String sqlStr="select * from t_bc_param_set where p_code like ? and company_id=? group by company_id";
        List<Map<String,Object>> mainTainTypeList=this.getJdbcDao().queryForMapListBySqlWithParams(sqlStr,code+"%",companyId);
        if(mainTainTypeList.size()>0){
            return mainTainTypeList;
        }
        return null;
    }

    /**
     * 查询公司及公司维修项目
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> findCompanyMainTainList() throws Exception {
        List<Map<String,Object>> lists=this.getJdbcDao().queryForMapListBySqlWithParams(
                "select company_id as companyId,GROUP_CONCAT(maint_project_name) as itemName from t_bc_maintain_type where maint_ref=? group by company_id","1");
        if(lists.size()>0){
            return lists;
        }
        return null;
    }

    /**
     * 查询工单内的公司信息
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String,Object>> findCompanyByWorkOrder() throws Exception {
        List<Map<String,Object>> lists=this.getJdbcDao().queryForMapListBySqlWithParams(
                "select company_id from t_crm_car_workorder group by company_id;");
        if(lists.size()>0){
            return lists;
        }
        return null;
    }

    /**
     * 查询车辆参数设置表，查询流失类设置
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String,Object>> findCompanyMaintainlsList() throws Exception{
        String sql="select bcset.`code`,GROUP_CONCAT(bcset.p_code) pCode,def.p_code defPcode,bcset.company_id companyId  from t_bc_param_set  bcset , t_bc_param_def def " +
                "where bcset.p_code=def.code and def.p_code='ls' group by bcset.company_id ";
        List<Map<String,Object>> lists=this.getJdbcDao().queryForMapListBySqlWithParams(sql);
//                "select company_id companyId,p_code pCode from t_bc_param_set where p_code='ls' GROUP BY company_id");
        if(lists.size()>0){
            return lists;
        }
        return null;
    }

    /**
     * 根据PCode查询公司信息
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String,Object>> findCompanyList(String code,String pcode)throws Exception{
        List<Map<String,Object>> lists=this.getJdbcDao().queryForMapListBySqlWithParams(
                "select company_id,p_code as code,`value` from t_bc_param_set where code=? and p_code=? GROUP BY company_id ",code,pcode);
        if(lists.size()>0){
            return lists;
        }
        return null;
    }

    /**
     *
     * @param pCode
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String,Object>> findCompanyByPCodeList(String pCode)throws Exception{
        List<Map<String,Object>> lists=this.getJdbcDao().queryForMapListBySql("select GROUP_CONCAT(`p_code`) codes," +
                "company_id from t_bc_param_set where p_code>=6 and p_code<=12 GROUP BY company_id");
        if(lists.size()>0){
            return lists;
        }
        return null;
    }

    @Override
    public List<Map<String,Object>> findCompanyByPCodeAndCompanyId(String pCode,String companyId)throws Exception{
        List<Map<String,Object>> mapList=this.getJdbcDao().queryForMapListBySqlWithParams(
                "select * from (select `p_code` as code,company_id from t_bc_param_set where p_code>=6 and p_code<=13  " +
                        "and company_id=?  order by `p_code`*1 DESC ) t",companyId);
        if(mapList.size()>0){
            return mapList;
        }
        return null;

    }

}
